package com.example.accessingdatajpa.controller;

import com.example.accessingdatajpa.pojo.Category;
import com.example.accessingdatajpa.pojo.Goods;
import com.example.accessingdatajpa.response.RestResponse;
import com.example.accessingdatajpa.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/app")
public class GoodsController {
    @Autowired
    final private IGoodsService goodsService;

    public GoodsController(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }

    //find all Goods
    @GetMapping(path = "/allGoods")
    public @ResponseBody RestResponse<List<Goods>> findAllGoods(){
        return goodsService.queryAllGoods();
    }

    //find goods by categoryId
    @GetMapping(path = "/byCategory/{categoryId}")
    public @ResponseBody RestResponse<List<Goods>> findAllByCategoryId(@PathVariable("categoryId") Long categoryId){
        return goodsService.queryGoodsByCategoryId(categoryId);
    }
    //fuzzy search goods by goods title
    @GetMapping(path = "/fuzzySearch/{goodsTitle}")
    public @ResponseBody RestResponse<List<Goods>> findAllByGoodsName(@PathVariable("goodsTitle") String goodsTitle){
        return goodsService.queryGoodsByGoodsName(goodsTitle);
    }

    //find hot goods
    @GetMapping(path = "/hotGoods")
    public @ResponseBody RestResponse<List<Goods>> findAllHotGoods(){
        return goodsService.queryHotGoods();
    }

    //find new goods
    @GetMapping(path = "/newGoods")
    public @ResponseBody RestResponse<List<Goods>> findAllNewGoods(){
        return goodsService.queryNewGoods();
    }

    //find other goods by category id
    @GetMapping(path = "/otherGoods/{categoryId}")
    public @ResponseBody RestResponse<List<Goods>> findOtherGoodsByCategoryId(@PathVariable("categoryId") Long categoryId){
        return goodsService.queryOtherGoods(categoryId);
    }

    //add new category
    @PostMapping(path = "/addGoods")
    public @ResponseBody RestResponse<String> addGoods(@RequestBody Goods goods){
        return goodsService.addNewGoods(goods);
    }
}
